//
//  LXLNewFeatureViewController.swift
//  Weibo-YQY-test
//
//  Created by Lang on 10/14/15.
//  Copyright © 2015 Lang. All rights reserved.
//

import UIKit
// 新特性界面的页面总数
private let count = 4

class LXLNewFeatureViewController: UIViewController, UIScrollViewDelegate {
    
    var pageControl: UIPageControl?

    override func viewDidLoad() {
        super.viewDidLoad()

        //初始化控件
        let scrollView = UIScrollView(frame: UIScreen.mainScreen().bounds)
        //指定背景颜色
        scrollView.backgroundColor = UIColor.whiteColor()
        // 设置分页
        scrollView.pagingEnabled = true
        // 隐藏水平滚动条
        scrollView.showsHorizontalScrollIndicator = false
        
        scrollView.delegate = self
        
        
        for i in 0..<count {
            //初始化子控件
            let imageView = UIImageView(image: UIImage(named: "new_feature_\(i+1)"))
            //设置大小
            imageView.size = scrollView.size
            imageView.x = CGFloat(i) * scrollView.width
            
            //添加子控件
            scrollView.addSubview(imageView)
            
            //判断是否是最后一个 view
            if i == count - 1{
                //是最后一个
                setupLastPage(imageView)
                
            }
            
            // 设置 scrollView内容的大小
            scrollView.contentSize = CGSizeMake(CGFloat(count) * scrollView.width, 0)
            //添加 scrollView
            view.addSubview(scrollView)
            
            
            // 添加一个 pageControl
            let pageCon = UIPageControl()
            pageCon.numberOfPages = count
            pageCon.centerX = scrollView.width * 0.5
            pageCon.y = SCREENH - 100
            
            pageCon.pageIndicatorTintColor = UIColor.blackColor()
            pageCon.currentPageIndicatorTintColor = UIColor.orangeColor()
            
            view.addSubview(pageCon)
            
            self.pageControl = pageCon
        }
        
        
    }

    //设置最后一张 imageView
    private func setupLastPage(imageView: UIImageView){
        // 开启用户交互
        imageView.userInteractionEnabled = true
        //进入按钮
        let enterButton = UIButton()
        
        //button的监听事件
        enterButton.addTarget(self, action: "enterButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        // 设置不同状态的背景图
        enterButton.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        enterButton.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        //设置标题
        enterButton.setTitle("进入微博", forState: UIControlState.Normal)
        //设置 frame
        enterButton.sizeToFit()
        enterButton.centerX = SCREENW * 0.5
        enterButton.y = SCREENH - 200
        
        imageView.addSubview(enterButton)
        
        //分享按钮
        //这里使用自定义按钮，是为了让按钮文字和选择框之间有一个间距
        let sharedButton = LXLSharedButton()
        //添加点击事件
        sharedButton.addTarget(self, action: "sharedButtonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        //设置文字的颜色
        sharedButton.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        //设置不同状态下的图片
        sharedButton.setImage(UIImage(named: "new_feature_share_false"), forState: UIControlState.Normal)
        sharedButton.setImage(UIImage(named: "new_feature_share_true"), forState: UIControlState.Selected)
        
        // 设置标题
        sharedButton.setTitle("分享到微博", forState: UIControlState.Normal)
        // 设置大小
        sharedButton.sizeToFit()
        sharedButton.centerX = enterButton.centerX
        sharedButton.y = enterButton.y - 50
        
        
        
        imageView.addSubview(sharedButton)
        
    }

    
    @objc private func enterButtonClick(){
        //获取到项目的 appDelegate
//        let delegate = UIApplication.sharedApplication().delegate as! AppDelegate
//        //取出 window, 切换根控制器
//        delegate.window?.rootViewController = LXLTabBarController()
        // 获取到项目的 appDelegate
//        let delegate = UIApplication.sharedApplication().delegate as! AppDelegate
        
//        //判断是否登录
//        let account = LXLUserAccount.loadUserAccount()
//        if let _ = account{
//            //如果登录---->进入到欢迎界面
//           delegate.window?.rootViewController = LXLWelcomeViewController()
//        }else{
//            //如果没有登录--->进入到登录界面
//            delegate.window?.rootViewController = LXLNavigationController(rootViewController: LXLOAuthViewController())
//        }
        
        LXLUserAccount.isLogin(loginSuccess: LXLWelcomeViewController(), noLogin: LXLOAuthViewController())
        
    }
    
    
    @objc private func sharedButtonClick(button: UIButton){
        button.selected = !button.selected
    }
    
    
/// scrollView的delegate
    func scrollViewDidScroll(scrollView: UIScrollView) {
        //计算滑动到第几页
        let page = scrollView.contentOffset.x / scrollView.width
        
        //设置pageControl当前的页面
        self.pageControl?.currentPage = Int(page + 0.5)
        
    }
    
}
